<?php
namespace application\datatypes;
use application\datatypes\FileUploadBase;
class ImageGallery extends FileUploadBase {
	public static $name = 'Image Gallery';
	public static $fields = array();	
	public static $filters = "'image/png', 'image/jpg', 'image/jpeg', 'image/gif'";
	public static $maxSize = '1mb';
	
	public static $displayParams = array(
		array(
			'label'=>'Display As',
			'name'=>'display',
			'type'=>'select',
			'defaultValue'=>'none',
			'options'=>array(
				array(
					'label'=>'Lightbox',
					'value'=>'lightbox'
				),
				array(
					'label'=>'Unordered List',
					'value'=>'ul'
				),
				array(
					'label'=>'No Format',
					'value'=>'none'
				)
			)
		),
		array(
			'label'=>'Image Width',
			'name'=>'width',
			'type'=>'text',
			'class'=>'thin'
		),
		array(
			'label'=>'Image Height',
			'name'=>'height',
			'type'=>'text',
			'class'=>'thin'
		),
		array(
			'label'=>'Image Crop Ratio',
			'name'=>'cropRatio',
			'type'=>'text',
			'class'=>'thin'
		),
		array(
			'label'=>'# to Display',
			'name'=>'count',
			'type'=>'text',
			'class'=>'thin'
		),
		array(
			'label'=>'Images Link to their Files',
			'name'=>'link',
			'type'=>'checkbox',
			'options'=>array(
				array(
					'label'=>'Link to Files',
					'value'=>'TRUE',
					'name'=>'link'
				)	
			)
		)
		
	);
	
	public static function onUpload($field) {
		?>
		file.li.prepend('<img src="<?=CN_URL?>?view=image&height=50&width=200&image=' + data.location + '"/><div style="clear:both;"></div>');
		<?php
	}
	
	public static function formHeader($field) {
		?>
		<?php
		parent::uploadJS($field);

	}
	
	public static function displayHeader($field = null) {
		Modulator()->includeScript('jquery');
		Modulator()->includeScript(getCommonFile('third-party/jquery.lightbox/js/jquery.lightbox.js'));
		?>
		<link rel="stylesheet" href="<?=getCommonFile('third-party/jquery.lightbox/css/jquery.lightbox.css')?>" type="text/css" media="screen" />
		<script type="text/javascript">
			$(document).ready(function() {
				$('#<?=$field->cf_column?>_lightbox a').lightBox();
			});
		</script>
		<?php
	}
	
	public static function buildExisting($file) {
		return '<img src="'.CN_URL.'?view=image&image='.urlencode('uploads/').$file.'&width=100&height=100"/>';
	}
	
	public static function prepareData($field, $data, $params) {
		$images = $data[$field->cf_column];
		$return = '';
		$display = $params['display'];
		$height = $params['height'];
		$width = $params['width'];
		$crop = $params['cropRatio'];
		$count = $params['count'];
		$link = $params['link'];
		$imageNumber = $params['imageNumber'];
		
		if($count > count($images)) {
			$count = count($images);
		}
		if(count($images)) {
			switch($display) {
				case 'lightbox':
					$return = array();
					if($count) {
						for($i=0;$i<$count;$i++) {
							$image = $images[$i];
							$location = DB()->getVar("SELECT `upload_filename` FROM `cn_uploads` WHERE `upload_id`='$image'");
							$return[] = '<a href="'.CN_URL.'uploads/'.$location.'" rel="lightbox['.$field->cf_column.']"><img src="'.CN_URL.'?view=image&image='.urlencode('uploads/').$location.'&height='.$height.'&width='.$width.'&cropratio='.$crop.'"/></a>';
						}
					}
					else {
						foreach($images as $image) {
							$location = DB()->getVar("SELECT `upload_filename` FROM `cn_uploads` WHERE `upload_id`='$image'");
							$return[] = '<a href="'.CN_URL.'uploads/'.$location.'" rel="lightbox['.$field->cf_column.']"><img src="'.CN_URL.'?view=image&image='.urlencode('uploads/').$location.'&height='.$height.'&width='.$width.'&cropratio='.$crop.'"/></a>';
						}
					}
					$return = '<div id="'.$field->cf_column.'_lightbox">'.implode("\n", $return).'</div>';
					break;
				case 'ul':
					$return = '<ul class="image-gallery">';
					if($count) {
						for($i=0;$i<$count;$i++) {
							$image = $images[$i];
							$location = DB()->getVar("SELECT `upload_filename` FROM `cn_uploads` WHERE `upload_id`='$image'");
							if($link) {
								$return .= '<li><a href="'.CN_URL.'uploads/'.$location.'">'.'<img src="'.CN_URL.'?view=image&image='.urlencode('uploads/').$location.'&height='.$height.'&width='.$width.'&cropratio='.$crop.'"/>'.'</a></li>';
							}
							else {
								$return .= '<li>'.'<img src="'.CN_URL.'?view=image&image='.urlencode('uploads/').$location.'&height='.$height.'&width='.$width.'&cropratio='.$crop.'"/>'.'</li>';
							}
						}
					}
					else {
						foreach($images as $image) {
							$location = DB()->getVar("SELECT `upload_filename` FROM `cn_uploads` WHERE `upload_id`='$image'");
							if($link) {
								$return .= '<li><a href="'.CN_URL.'uploads/'.$location.'">'.'<img src="'.CN_URL.'?view=image&image='.urlencode('uploads/').$location.'&height='.$height.'&width='.$width.'&cropratio='.$crop.'"/>'.'</a></li>';
							}
							else {
								$return .= '<li>'.'<img src="'.CN_URL.'?view=image&image='.urlencode('uploads/').$location.'&height='.$height.'&width='.$width.'&cropratio='.$crop.'"/>'.'</li>';
							}
						}
					}
					$return .= '</ul>';
					break;
				case 'none':
				default:
					if($imageNumber) {
						$image = $images[$imageNumber - 1];
						$location = DB()->getVar("SELECT `upload_filename` FROM `cn_uploads` WHERE `upload_id`='$image'");
						if($link) {
							$return .= '<a href="'.CN_URL.'uploads/'.$location.'">'.'<img src="'.CN_URL.'?view=image&image='.urlencode('uploads/').$location.'&height='.$height.'&width='.$width.'&cropratio='.$crop.'"/>'.'</a>';
						}
						else {
							$return .= '<img src="'.CN_URL.'?view=image&image='.urlencode('uploads/').$location.'&height='.$height.'&width='.$width.'&cropratio='.$crop.'"/>';
						}
					}
					elseif($count) {
						for($i=0;$i<$count;$i++) {
							$image = $images[$i];
							$location = DB()->getVar("SELECT `upload_filename` FROM `cn_uploads` WHERE `upload_id`='$image'");
							if($link) {
								$return .= '<a href="'.CN_URL.'uploads/'.$location.'">'.'<img src="'.CN_URL.'?view=image&image='.urlencode('uploads/').$location.'&height='.$height.'&width='.$width.'&cropratio='.$crop.'"/>'.'</a>';
							}
							else {
								$return .= '<img src="'.CN_URL.'?view=image&image='.urlencode('uploads/').$location.'&height='.$height.'&width='.$width.'&cropratio='.$crop.'"/>';
							}
						}
					}
					else {
						foreach($images as $image) {
							$location = DB()->getVar("SELECT `upload_filename` FROM `cn_uploads` WHERE `upload_id`='$image'");
							if($link) {
							
								$return .= '<a href="'.CN_URL.'uploads/'.$location.'">'.'<img src="'.CN_URL.'?view=image&image='.urlencode('uploads/').$location.'&height='.$height.'&width='.$width.'&cropratio='.$crop.'"/>'.'</a>';
							}
							else {
								$return .= '<img src="'.CN_URL.'?view=image&image='.urlencode('uploads/').$location.'&height='.$height.'&width='.$width.'&cropratio='.$crop.'"/>';
							}
						}
					}
					break;
			
			}
			
		}
		return $return;
	}
	

}
?>